﻿using System;
using System.Linq;
using Notoric.Gui.Windows;
using System.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that deletes a given project part.
    /// </summary>
    /// <remarks>
    /// The command accepts the deleted part as its parameter.
    /// The command can execute if the parameter is not <see langword="null"/>
    /// and if it implements the <see cref="ViewModel.IProjectExplorerItem"/>
    /// interface.
    /// </remarks>
    public class DeleteProjectPartCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="DeleteProjectPartCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// The main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public DeleteProjectPartCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return parameter != null && parameter is ViewModel.IProjectExplorerItem;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            if (parameter is ViewModel.Elements.Composition)
            {
                var representations = from score in ApplicationWindow.ProjectExplorer.Projects[0].Scores
                                      from page in score.Pages
                                      from section in page.Sections
                                      where section is ViewModel.Elements.MusicSection && ((ViewModel.Elements.MusicSection)section).PresentedComposition == parameter
                                      select section;

                if (representations.Count() > 0)
                {
                    string pages = "";
                    foreach (var section in representations)
                        pages += String.Format("{0}\\{1}", section.FindOwner<ViewModel.Elements.Score>().Name ,section.FindOwner<ViewModel.Elements.Page>().Name);

                    MessageBox.Show(
                        String.Format("The \"{0}\" cannot be deleted, because it is presented on the following pages:\n{1}\nPlease remove these sections first.",
                        ((ViewModel.Elements.Composition)parameter).Name,
                        pages
                        ),
                        "Notoric",
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation
                        );
                    return;
                }
            }
            else if (parameter is ViewModel.Elements.Resource)
            {
                var representations = from score in ApplicationWindow.ProjectExplorer.Projects[0].Scores
                                      from page in score.Pages
                                      from section in page.Sections
                                      where section is ViewModel.Elements.ImageSection && ((ViewModel.Elements.ImageSection)section).ImageFile == parameter
                                      select section;

                if (representations.Count() > 0)
                {
                    string pages = "";
                    foreach (var section in representations)
                        pages += String.Format("{0}\\{1}", section.FindOwner<ViewModel.Elements.Score>().Name, section.FindOwner<ViewModel.Elements.Page>().Name);

                    MessageBox.Show(
                        String.Format("The \"{0}\" cannot be deleted, because it is presented on the following pages:\n{1}\nPlease remove these sections first.",
                        ((ViewModel.Elements.Resource)parameter).Name,
                        pages
                        ),
                        "Notoric",
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation
                        );
                    return;
                }
            }

            var cmd = new ViewModel.DeleteProjectPartCommand(
                ApplicationWindow.ProjectExplorer.Projects[0].UndoStack,
                ApplicationWindow.ProjectExplorer.Projects[0].RedoStack
                )
                {
                    DeletedPart = (ViewModel.IProjectExplorerItem)parameter
                };

            cmd.Execute(null);
        }
        
        #endregion
    }
}
